package com.megvii.koala.components;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.megvii.koala.R;

public class TitleView extends RelativeLayout implements View.OnClickListener
{
    private View returnFrame;

    private TextView returnText;

    private ImageView returnImage;

    private View placeHolder;

    private RelativeLayout titleFrame;

    private TextView title;

    private View functionFrame;

    private TextView functionText;

    private ImageView functionImage;

    private TitleListener listener = null;

    private boolean titleCenter = true;

    private boolean enableReturn = true;

    private boolean enableFunction = false;

    private Style returnStyle = Style.BUTTON_STYLE_IMAGE;

    private Style functionStyle = Style.BUTTON_STYLE_IMAGE;

    public TitleView(Context context)
    {
        super(context);
        init(null);
    }

    public TitleView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(attrs);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(attrs);
    }

    private void init(AttributeSet attrs)
    {
        TypedArray array = null;
        int layout_res_id = 0;
        if (attrs != null)
        {
            array = getContext().obtainStyledAttributes(attrs, R.styleable.TitleView);
            layout_res_id = array
                    .getResourceId(R.styleable.TitleView_title_layout, R.layout.title_layout);
        }
        inflate(getContext(), layout_res_id, this);
        this.returnFrame = findViewById(R.id.title_layout_leftLinear);
        this.returnText = (TextView) findViewById(R.id.title_layout_text);
        this.returnImage = (ImageView) findViewById(R.id.title_layout_leftImage);
        this.placeHolder = findViewById(R.id.title_layout_placeHolder);
        this.titleFrame = (RelativeLayout) findViewById(R.id.title_layout_centLinear);
        this.title = (TextView) findViewById(R.id.title_layout_centtext);
        this.functionFrame = findViewById(R.id.title_layout_rightRel);
        this.functionText = (TextView) findViewById(R.id.title_layout_rightText);
        this.functionImage = (ImageView) findViewById(R.id.title_layout_rightImage);
        if (array != null)
        {
            setTitleCenter(array.getBoolean(R.styleable.TitleView_title_center, false));
            setTitleText(array.getString(R.styleable.TitleView_title_text));
            setTitleTextVisible(array.getBoolean(R.styleable.TitleView_title_text_visible, true));
            setReturnEnable(array.getBoolean(R.styleable.TitleView_return_enable, true));
            setReturnStyle(Style.getEnum(array.getInt(R.styleable.TitleView_return_style, 0)));
            setReturnText(array.getString(R.styleable.TitleView_return_text));
            setFunctionEnable(array.getBoolean(R.styleable.TitleView_function_enable, false));
            setFunctionStyle(Style.getEnum(array.getInt(R.styleable.TitleView_function_style, 0)));
            setFunctionText(array.getString(R.styleable.TitleView_function_text));
            setFunctionImage(
                    array.getResourceId(R.styleable.TitleView_function_image, R.drawable.add));
            this.setTextSize(array.getDimension(R.styleable.TitleView_text_size1,
                    getResources().getDimension(R.dimen.title_textsize)));
        }
        this.returnFrame.setOnClickListener(this);
        this.functionFrame.setOnClickListener(this);
        this.titleFrame.setOnClickListener(this);
    }

    public void setTitleListener(TitleListener listener)
    {
        this.listener = listener;
    }

    public void setTitleCenter(boolean center)
    {
        titleCenter = center;
        updateLeft();
    }

    public void setTitleTextVisible(boolean visible)
    {
        if (visible)
        {
            titleFrame.setVisibility(View.VISIBLE);
        }
        else
        {
            titleFrame.setVisibility(View.GONE);
        }
    }

    public void setReturnEnable(boolean enable)
    {
        enableReturn = enable;
        if (enable)
        {
            returnFrame.setVisibility(View.VISIBLE);
        }
        else
        {
            returnFrame.setVisibility(View.GONE);
        }
        updateLeft();
    }

    public void setReturnStyle(Style style)
    {
        returnStyle = style;
        switch (style)
        {
            case BUTTON_STYLE_TEXT:
                returnText.setVisibility(View.VISIBLE);
                returnImage.setVisibility(View.GONE);
                break;
            case BUTTON_STYLE_IMAGE:
            default:
                returnText.setVisibility(View.GONE);
                returnImage.setVisibility(View.VISIBLE);
                break;
        }
    }

    public void setReturnText(String text)
    {
        returnText.setText(text);
    }

    public void setReturnImage(int resId)
    {
        returnImage.setImageResource(resId);
    }

    public void setFunctionEnable(boolean enable)
    {
        enableFunction = enable;
        if (enable)
        {
            functionFrame.setVisibility(View.VISIBLE);
        }
        else
        {
            functionFrame.setVisibility(View.GONE);
        }
    }

    public void setFunctionStyle(Style style)
    {
        functionStyle = style;
        switch (style)
        {
            case BUTTON_STYLE_TEXT:
                functionText.setVisibility(View.VISIBLE);
                functionImage.setVisibility(View.GONE);
                break;
            case BUTTON_STYLE_IMAGE:
            default:
                functionText.setVisibility(View.GONE);
                functionImage.setVisibility(View.VISIBLE);
                break;
        }
    }

    public void setFunctionText(String text)
    {
        functionText.setText(text);
    }

    public void setFunctionImage(int resId)
    {
        functionImage.setImageResource(resId);
    }

    public String getTitleText()
    {
        return title.getText().toString();
    }

    public void setTitleText(String text)
    {
        title.setText(text);
    }

    public void setTextSize(float size)
    {
        returnText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        title.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        functionText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }

    public int getFunctionFrameId()
    {
        return R.id.title_layout_rightRel;
    }

    public int getTitleHeightInPixel()
    {
        return (int) getResources().getDimension(R.dimen.title_hight);
    }

    private void updateLeft()
    {
        LayoutParams params = (LayoutParams) titleFrame.getLayoutParams();
        if (titleCenter)
        {
            params.addRule(RelativeLayout.RIGHT_OF, 0);
            params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        }
        else
        {
            params.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
            if (enableReturn)
            {
                placeHolder.setVisibility(View.GONE);
                params.addRule(RelativeLayout.RIGHT_OF, R.id.title_layout_leftLinear);
            }
            else
            {
                placeHolder.setVisibility(View.VISIBLE);
                params.addRule(RelativeLayout.RIGHT_OF, R.id.title_layout_placeHolder);
            }
        }
        titleFrame.setLayoutParams(params);
    }

    @Override
    public void onClick(View v)
    {
        if (listener != null)
        {
            int id = v.getId();
            if (id == R.id.title_layout_leftLinear)
            {
                listener.onClickReturn();
            }
            else if (id == R.id.title_layout_rightRel)
            {
                listener.onClickFunction();
            }
            else if (id == R.id.title_layout_centLinear)
            {
                if (!titleCenter)
                {
                    listener.onClickReturn();
                }
            }
        }
    }

    public enum Style
    {
        BUTTON_STYLE_TEXT, BUTTON_STYLE_IMAGE;

        static Style getEnum(int ordinal)
        {
            return Style.values()[ordinal];
        }
    }

    public interface TitleListener
    {
        public void onClickReturn();

        public void onClickFunction();
    }

}
